﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Model;
using BLL;
using Common;

public partial class Manager_UserMessagerInfo : BasePage
{
    User user = new User();
    UserBLL bll = new UserBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        TestPurview(HttpContext.Current, 1);
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        string id = Request.QueryString["id"];

        if (id != null)
        {
            string[] uidStr = id.Split(',');
            string uid = uidStr[0];
            user = bll.GetModel(Convert.ToInt32(uid));
            this.txtUserName.Text = user.userName;
            this.txtCompany.Text = user.company;
            this.txtLinkman.Text = user.linkman;
            this.txtQQ.Text = user.qq;
            this.txtEMail.Text = user.email;
            this.txtTel.Text = user.tel;
            this.txtMobilePhone.Text = user.mobilePhone;
            this.txtFax.Text = user.fax;
            this.ddlState.SelectedIndex = user.state;
            this.ddlType.SelectedIndex = user.type;
            this.txtComment.Text = user.comment;
            this.txtBriefIntro.InnerHtml = user.briefIntro;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];

        user.userName = this.txtUserName.Text;
        user.password = Help.MD5(this.txtPwd.Text);
        user.company = this.txtCompany.Text;
        user.linkman = this.txtLinkman.Text;
        user.qq = this.txtQQ.Text;
        user.email = this.txtEMail.Text;
        user.tel = this.txtTel.Text;
        user.mobilePhone = this.txtMobilePhone.Text;
        user.fax = this.txtFax.Text;
        user.state = this.ddlState.SelectedIndex;
        user.type = this.ddlType.SelectedIndex;
        user.comment = this.txtComment.Text;
        user.briefIntro = this.txtBriefIntro.Value;
        if (id == "")
        {
            if (bll.Add(user))
            {
                Response.Write("<script>alert('添加成功！')</script>");
                Response.Redirect("UserMessager.aspx");
            }
            else
            {
                Response.Write("<script>alert('添加失败！')</script>");
            }
        }
        else
        {
            string[] uidStr = id.Split(',');
            user.uid = Convert.ToInt32(uidStr[0]);
            if (bll.Update(user))
            {
                Response.Write("<script>alert('修改成功！')</script>");
                Response.Redirect("UserMessager.aspx");
            }
            else
            {
                Response.Write("<script>alert('修改失败！')</script>");
            }
        }
    }
}
